package com.work.device.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.work.device.pojo.Addstatus;
import com.work.device.pojo.Device;
import com.work.device.service.DeviceService;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YuanGan
 * @Description : 后台设备的增删改查
 * @Date 2020/10/12
 **/
@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    /**
     * 根据设备名称分页查询设备
     *
     * @param map 查询条件map
     * @return Result 返回数据
     */
    @GetMapping("/findAll")
    public Map<String, Object> findAll(@RequestParam Map<String, Object> map) {
        Map<String, Object> resultMap = null;
        try {
            resultMap = deviceService.pageQuery(map);
            resultMap.put("flag", true);
            resultMap.put("code", StatusCode.FIND_Ok);
            resultMap.put("message", "查询设备成功");
            return resultMap;

        } catch (Exception e) {
            e.printStackTrace();
            resultMap = new HashMap<>();
            resultMap.put("flag", false);
            resultMap.put("code", StatusCode.FIND_ERROR);
            resultMap.put("message", "查询设备失败");
        }
        return resultMap;
    }

    /**
     * 新增设备的功能
     *
     * @param map 添加参数
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody Map<String, Object> map) {
        try {
            Addstatus addstatus = deviceService.add(map);
            if (Addstatus.ADDSUCCESS.equals(addstatus)) {
                //添加成功
                return new Result(true, StatusCode.ADD_OK, "新增设备成功");
            } else if (Addstatus.ADDPARAMERROR.equals(addstatus)) {
                return new Result(false, StatusCode.ADD_DEVICE_MESSAGE_ERROR, "参数异常");
            } else if (Addstatus.ADDREPEAT.equals(addstatus)) {
                return new Result(false, StatusCode.ADD_ERROR, "设备已有,请勿重复添加");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ADD_ERROR, "新增设备失败");
    }

    /**
     * 更新设备功能
     *
     * @param device 设备
     * @return 更新设备结果
     */
    @PostMapping("/update")
    public Result update(@RequestBody Device device) {
        try {
            Integer result = deviceService.update(device);
            if (result == 1) {
                return new Result(true, StatusCode.UPDATE_Ok, "更新设备成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.UPDATE_ERROR, "更新设备失败");
    }

    /**
     * 删除设备功能
     *
     * @param id 设备的编号
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        try {
            long idLong = Long.parseLong(id + "");
            Integer result = deviceService.delete(idLong);
            if (result == 1) {
                return new Result(true, StatusCode.DELETE_OK, "删除设备成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.DELETE_ERROR, "删除设备失败");
    }

    /**
     * 批量删除设备功能
     *
     * @param ids 设备的编号集合
     * @return 批量删除结果
     */
    @DeleteMapping("/deleteAll")
    public Result deleteAll(@RequestBody Map<String, Object> ids) {
        try {
            List<Long> idsList = (List<Long>) ids.get("ids");
            deviceService.deleteAll(idsList);
            return new Result(true, StatusCode.DELETE_OK, "批量删除设备成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.DELETE_ERROR, "批量删除设备失败");
    }
}
